<?php
    class WZ_Request extends Zend_Controller_Request_Http
    {
        static $post;
        static $get;
        static $cookie;
        static $session;
        static $_instance;
		
		// singleton
		public static function &getInstance() 
		{
            if (!static::$_instance instanceof self) {
				static::$_instance = new self;
			}
            return static::$_instance;
		}
        
        public static function init()
        {
            static::setWZPost();
            static::setWZGet();
            static::setWZCookie();
            static::setWZSession();
        }
        
        public static function paramsValues($index = null, $default = null)
        {
            $input = static::hydrate();
            return arrayGet($input, $index, $default);
        }
        
        public static function serverValues($index = null, $default = null)
        {
            return (func_num_args() === 0) ? $_SERVER : arrayGet($_SERVER, \WZ_Inflector::upper($index), $default);
        }
        
        public static function getValues($index = null, $default = null)
        {
            return (func_num_args() === 0) ? $_GET : arrayGet($_GET, $index, $default);
        }
        
        public static function postValues($index = null, $default = null)
        {
            return (func_num_args() === 0) ? $_POST : arrayGet($_POST, $index, $default);
        }
        
        public static function cookieValues($index = null, $default = null)
        {
            return (func_num_args() === 0) ? $_COOKIE : arrayGet($_COOKIE, $index, $default);
        }
        
        public static function fileValues($index = null, $default = null)
        {
            return (func_num_args() === 0) ? $_FILES : arrayGet($_FILES, $index, $default);
        }
        
        public static function method($default = 'GET')
        {
            return static::serverValues('HTTP_X_HTTP_METHOD_OVERRIDE', static::serverValues('REQUEST_METHOD', $default));
        }
        
        protected static function hydrate()
        {
            $return = array_merge($_GET, $_POST);

            if (static::method() == 'PUT' || static::method() == 'DELETE') {
                parse_str(file_get_contents('php://input'), $put);
                $return = array_merge($return, $put);
            }
            return $return;
        }
        
        public static function setWZPost($array = false)
        {
            if (isset($_POST)) {
                if (count($_POST)) {
                    foreach ($_POST as $key => $value) {
                        $key = strtolower($key);
                        if (false === $array) {
                            static::$post = new \WZ_Request_Post;
                            static::$post->$key = $value;
                        } else {
                            static::$post[$key] = $value;
                        }
                    }
                }
            }
        }
        
        public static function setWZGet($array = false)
        {
            if (isset($_GET)) {
                if (count($_GET)) {
                    foreach ($_GET as $key => $value) {
                        $key = strtolower($key);
                        if (false === $array) {
                            static::$get = new \WZ_Request_Get;
                            static::$get->$key = $value;
                        } else {
                            static::$get[$key] = $value;
                        }
                    }
                }
            }
        }
        
        public static function setWZCookie($array = false)
        {
            if (isset($_COOKIE)) {
                if (count($_COOKIE)) {
                    foreach ($_COOKIE as $key => $value) {
                        $key = strtolower($key);
                        if (false === $array) {
                            static::$cookie = new \WZ_Request_Cookie;
                            static::$cookie->$key = $value;
                        } else {
                            static::$cookie[$key] = $value;
                        }
                    }
                }
            }
        }
        
        public static function setWZSession($array = false)
        {
            if (isset($_SESSION)) {
                if (count($_SESSION)) {
                    foreach ($_SESSION as $key => $value) {
                        $key = strtolower($key);
                        if (false === $array) {
                            static::$session = new \WZ_Request_Session;
                            static::$session->$key = $value;
                        } else {
                            static::$session[$key] = $value;
                        }
                    }
                }
            }
        }
        
        public static function getWZPost($key = null)
        {
            if (null === $key) {
                return static::$post;
            } else {
                if (isset(static::$post->$key)) {
                    return static::$post->$key;
                }
            }
            return null;
        }
        
        public static function getWZGet($key = null)
        {
            if (null === $key) {
                return static::$get;
            } else {
                if (isset(static::$get->$key)) {
                    return static::$get->$key;
                }
            }
            return null;
        }
        
        public static function getWZCookie($key = null)
        {
            if (null === $key) {
                return static::$cookie;
            } else {
                if (isset(static::$cookie->$key)) {
                    return static::$cookie->$key;
                }
            }
            return null;
        }
        
        public static function getWZSession($key = null)
        {
            if (null === $key) {
                return static::$session;
            } else {
                if (isset(static::$session->$key)) {
                    return static::$session->$key;
                }
            }
            return null;
        }
        
    }
    
    class WZ_Request_Post {};
    class WZ_Request_Get {};
    class WZ_Request_Cookie {};
    class WZ_Request_Session {};